package cn.wen.base.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import cn.wen.base.swipeback.app.SwipeBackActivity;


/**
 * Activity的基类
 */

public abstract class BaseActivity extends SwipeBackActivity {
    /**
     * 用户设置的ContentView
     */
    protected View mContentView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManager.getInstance().attach(this);
        // 1. 初始化数据，包括上个页面传递过来的数据在这个方法做
        initData(savedInstanceState);
        // 2. 创建ContentView
        mContentView = createContentView();
        setContentView(mContentView);
        // 3.1创建标题
        initTitle();
        // 3.2绑定View
        bindView(mContentView);
        // 4. 初始化View
        initView(mContentView);
        // 5. 初始化网络
        initNet();
    }


    /**
     * 1. 初始化数据，包括上个页面传递过来的数据在这个方法做
     */
    protected void initData(Bundle savedInstanceState) {

    }

    /**
     * 2.创建ContentView
     */
    private View createContentView() {
        Object layout = getContentLayout();
        View contentView = null;
        if (layout instanceof View) {
            contentView = (View) layout;
        } else if (layout instanceof Integer) {
            contentView = getLayoutInflater().inflate((Integer) layout, null, false);
        }
        if (contentView == null) {
            new IllegalArgumentException("getContentLayout must View or LayoutId");
        }
        return contentView;
    }

    protected abstract Object getContentLayout();

    /**
     * 3.1 创建标题
     */
    protected void initTitle() {

    }

    /**
     * 3.2绑定View
     */
    protected void bindView(View contentView) {

    }

    /**
     * 4. 初始化View
     */
    protected void initView(View contentView) {

    }

    /**
     * 5. 初始化网络
     */
    public void initNet() {

    }

    @Override
    protected void onDestroy() {
        unbindView(mContentView);
        ActivityManager.getInstance().detach(this);
        InputMethodUtils.fixInputMethodManagerLeak(this);
        mContentView = null;
        super.onDestroy();
    }

    /**
     * 解绑contentView
     */
    protected void unbindView(View contentView) {

    }

    /**
     * 打开Activity
     */
    public final void startActivity(Class<? extends Activity> clazz) {
        startActivity(clazz, null);
    }

    /**
     * 打开Activity
     */
    public final void startActivity(Class<?> clazz, @Nullable Bundle options) {
        Intent intent = new Intent(this, clazz);
        if (options != null) {
            intent.putExtras(options);
        }
        startActivity(intent);
    }

    public final Activity getAppActivity() {
        return this;
    }

    /**
     * 设置TextView
     */
    public void setText(TextView textView, CharSequence text) {
        if (textView != null && text != null) {
            textView.setText(text);
        }
    }
}
